<template>
  <div class="uploadImg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :before-upload="beforeAvatarUpload"
      :http-request="httpRequest"
    >
      <img
        v-if="value"
        :src="value"
        class="avatar"
        @click.stop.prevent="isShow = true"
        @globalImgerror="defaultImg"
      >
      <i v-if="!loading" class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
    <!-- 删除按钮 -->
    <div v-if="value" class="del" @click="del">X</div>
    <!-- 预览 -->
    <el-dialog title="图片预览" width="500px" :visible.sync="isShow">
      <img style="width: 100%" :src="value" alt="">
    </el-dialog>
    <!-- 进度条 -->
    <!-- <transiton name="fade"> -->
    <el-progress
      v-if="loading"
      class="progress"
      :width="progressWidth"
      type="circle"
      :percentage="percentage"
    />
    <!-- </transiton> -->
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDX1MVQ6f2B5mHZhigWCgz5zf7SgkiYEi3',
  SecretKey: 'CJNNKCxXzRs77sPjoeLq79xYx895tX5S'
})
export default {
  name: 'UploadImg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      progressWidth: 160,
      defaultImg: require('@/assets/common/header.jpg'),
      percentage: 0,
      loading: false,
      isShow: false
      //   imageUrl: ''
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      return true
    },
    httpRequest(res) {
      this.loading = true
      this.percentage = 0
      cos.putObject(
        {
          Bucket: 'hr77-1308947902' /* 必须存储桶名 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: res.file.uid + '' /* 必须 要求是字符串 */,
          StorageClass: 'STANDARD',
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // 上传进度
            this.percentage = progressData.percent * 100
            if (progressData.percent === 1) {
              setTimeout(() => {
                this.loading = false
              }, 1000)
            }
            // console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传成功的回调 err:代表错误, 没有错误就代表成功  data:上传成功的接口返回值
          console.log(err || data)
          //   this.imageUrl = 'http://' + data.Location
          this.$emit('input', 'http://' + data.Location)
        }
      )
    },
    del() {
      this.$emit('input', '')
    }
  }
}
</script>

<style lang="scss" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #ccc !important;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  border: none;
  position: relative;
  width: 178px;
  height: 178px;
  &:hover {
    .del {
      display: block;
    }
  }
  .del {
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
    display: none;
  }
  .progress {
    position: absolute;
    top: 7px;
    right: 7px;
  }
}
</style>
